package cn.edu.pzhu.javaweb.controller;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;

import cn.edu.pzhu.javaweb.pojo.User;
import cn.edu.pzhu.javaweb.pojo.Userinfo;

/**
 * Servlet implementation class RegistServlet
 */
@WebServlet("/regist") //访问当前网址,以"/"表示servlet上下文，表示当前项目根路径
public class RegistServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RegistServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
//		getWriter() 
//		request.getContextPath() 获取上下文路径
//		设置输入编码格式为UTF-8		response.setCharacterEncoding("utf-8");
//		设置输出编码格式为UTF-8
		response.setContentType("text/html; charset=UTF-8");
		
//	获取注册界面的值
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		String email = request.getParameter("email");
		String sex = request.getParameter("sex");
		String[] types = request.getParameterValues("type");
		String captcha = request.getParameter("captcha");
		
//		对比验证码的信息，忽略大小写的比较
		if ("gbcw".equalsIgnoreCase(captcha))) {
			//验证码正确，跳转到登录页面
		}else {
			//验证码错误，跳转到注册页面
		}
		
//		将数据全部封装为一个json对象
		User user = new User(username,password,1);
		Userinfo userinfo = new Userinfo(username,email,Integer.valueOf(sex),JSON.toJSONString(types));
		
		response.getWriter().append(user.toString());
		response.getWriter().append(userinfo.toString());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
